package com.example.shop_2212

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.shop_2212.databinding.ActivityMainBinding
import com.example.shop_2212.fgviewmodel.MineViewModel

@Route(path = "/app/MainActivity")
class MainActivity : BaseActivity<ActivityMainBinding,MineViewModel>() {
    override fun initView() {
        viewModel.userInfo()
        //点击修改密码弹出对话框，修改密码
        binding.btnPwd.setOnClickListener {
            val inflate = LayoutInflater.from(this).inflate(R.layout.update_pwd, null)
            inflate.findViewById<TextView>(R.id.tv_phone).text = "15231282667"
            val alertDialog = AlertDialog.Builder(this)
                .setTitle("修改密码")
                .setView(inflate)
                .create()
            //发送验证码点击事件
            inflate.findViewById<Button>(R.id.btn_getCode).setOnClickListener {
                viewModel.sendCode(inflate.findViewById<TextView>(R.id.tv_phone).text.toString())
            }
            //保存点击事件
            inflate.findViewById<Button>(R.id.btn_save).setOnClickListener {
                viewModel.password(inflate.findViewById<TextView>(R.id.tv_phone).text.toString(),
                    inflate.findViewById<EditText>(R.id.et_code).text.toString(),
                    inflate.findViewById<EditText>(R.id.et_pwd).text.toString())

                alertDialog.dismiss()
            }
            //取消点击事件
            inflate.findViewById<Button>(R.id.btn_cancel).setOnClickListener {
                alertDialog.dismiss()
            }
            alertDialog.show()
        }
    }

    override fun initData() {
        viewModel.infoSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                binding.user = it.data
            }
        }
        viewModel.infoFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.codeSuccess.observe(this){
            if(it.code==Const.RESULT_OK){
                ToastUtils.showLong(it.data.content.substring(8,14))
            }
        }
        viewModel.pwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<MineViewModel>  = MineViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_main

}